import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:syncfusion_flutter_datepicker/datepicker.dart';
import 'package:yp/config/config.dart';
import 'package:yp/pages/my_invite/widgets/filter_button.dart';

import 'logic.dart';
import 'state.dart';

class MyInvitePage extends StatelessWidget {
  final MyInviteLogic logic = Get.put(MyInviteLogic());
  final MyInviteState state = Get.find<MyInviteLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.black),
        title: Text(
          "邀请列表",
          style: TextStyle(
            color: Colors.black,
            fontSize: 32.sp,
          ),
        ),
        centerTitle: true,
        elevation: 0,
      ),
      body: GetBuilder<MyInviteLogic>(
        builder: (logic) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              /// tab 切换 选择时间
              Container(
                padding: EdgeInsets.symmetric(
                  vertical: 30.w,
                  horizontal: 20.w,
                ),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border(
                    bottom: BorderSide(
                      color: Color(0xffcccccc),
                      width: 0.2,
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        FilterButton(
                          title: "全部",
                          isSelected: state.selectIndex == 1,
                          onTap: () {
                            logic.onSelect(1);
                          },
                        ),
                        FilterButton(
                          title: "今日",
                          isSelected: state.selectIndex == 2,
                          onTap: () {
                            logic.onSelect(2);
                          },
                        ),
                        FilterButton(
                          title: "昨日",
                          isSelected: state.selectIndex == 3,
                          onTap: () {
                            logic.onSelect(3);
                          },
                        ),
                        FilterButton(
                          title: "近七日",
                          isSelected: state.selectIndex == 4,
                          onTap: () {
                            logic.onSelect(4);
                          },
                        ),
                      ],
                    ),
                    GestureDetector(
                      onTap: () {
                        simpleDialog(context);
                      },
                      child: Row(
                        children: [
                          state.startTime == ''
                              ? Text(
                                  "自定义时间",
                                  style: TextStyle(
                                    fontSize: 22.sp,
                                    color: Color(0xff555555),
                                  ),
                                )
                              : Container(
                                  constraints: BoxConstraints(
                                    maxWidth: 115.w,
                                  ),
                                  child: Text(
                                    "${state.startTime} - ${state.endTime}",
                                    style: TextStyle(
                                      fontSize: 22.sp,
                                      color: Color(0xffEA5504),
                                    ),
                                  ),
                                ),
                          Icon(
                            Icons.keyboard_arrow_down_outlined,
                            color: Color(0xffADADAD),
                            size: 16,
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),

              /// title
              Container(
                padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
                child: RichText(
                  text: TextSpan(
                    children: [
                      TextSpan(
                        text: '共',
                        style: TextStyle(
                          color: Color(0xff999999),
                          fontSize: 28.w,
                        ),
                      ),
                      TextSpan(
                        text: '${state.totalCount}',
                        style: TextStyle(
                          color: Color(0xff222222),
                          fontSize: 32.w,
                        ),
                      ),
                      TextSpan(
                        text: '个邀请成功${state.from=='invite_koc' ? '的达人' : '会员'}',
                        style: TextStyle(
                          color: Color(0xff999999),
                          fontSize: 28.w,
                        ),
                      ),
                    ],
                  ),
                ),
              ),

              /// listview
              Expanded(
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 26.w),
                  child: SmartRefresher(
                    controller: state.controller1,
                    onRefresh: logic.refreshData,
                    onLoading: logic.loadMoreData,
                    enablePullUp: true,
                    enablePullDown: true,
                    child: ListView.builder(
                      shrinkWrap: true,
                      itemBuilder: (ctx, i) {
                        return buildListItem(ctx, i);
                      },
                      itemCount: state.inviteList.length,
                    ),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  //  时间弹窗
  simpleDialog(context) async {
    var result = await showDialog(
        barrierDismissible: false, // 表示点击灰色背景的时候是否消失弹出框
        context: context,
        builder: (context) {
          return SimpleDialog(
            children: <Widget>[
              Container(
                width: 600.w,
                height: 500.w,
                child: SfDateRangePicker(
                  onSelectionChanged: logic.onSelectionChanged,
                  selectionMode: DateRangePickerSelectionMode.range,
                  initialSelectedRange: PickerDateRange(
                      DateTime.now().subtract(const Duration(days: 4)),
                      DateTime.now().add(const Duration(days: 3))),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  FlatButton(
                    child: Text("取消"),
                    onPressed: () {
                      Navigator.of(context).pop("Cancel");
                    },
                  ),
                  FlatButton(
                    child: Text(
                      "确定",
                      style: TextStyle(
                        color: Config.mainColor,
                      ),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop("Ok");
                      logic.submitTime();
                    },
                  )
                ],
              )
            ],
          );
        });
    print(result);
  }

  GestureDetector buildListItem(ctx, i) {
    var item = state.inviteList[i];
    return GestureDetector(
      onTap: () {
        // if(item['apply_status_num_id'] == 1){
        //   Get.toNamed(RouteConfig.kocAudit, parameters: {
        //     "status":item['apply_status_num_id'] == '1'? '0' : '2',
        //     "mobile_phone":item['mobile_phone'],
        //     "leader_usr_name":item['leader_usr_name'],
        //     "leader_usr_num_id":item['leader_usr_num_id'].toString(),
        //   }).then((value) {
        //     if(value != '' && value != null){
        //       //刷新列表页
        //       logic.onReady();
        //       logic.update();
        //     }
        //   });
        // }else if(item['apply_status_num_id'] == 2){
        //   Get.toNamed(RouteConfig.userOrderList, parameters: {
        //     "status": i.toString(),
        //     "mobile_phone":item['mobile_phone'],
        //     "leader_usr_name":item['leader_usr_name'],
        //     "leader_usr_num_id":item['leader_usr_num_id'].toString(),
        //   }).then((value) {
        //     if(value != '' && value != null){
        //       //刷新列表页
        //       logic.onReady();
        //       logic.update();
        //     }
        //   });
        // }
      },
      child: Container(
        height: 160.w,
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        margin: EdgeInsets.only(
          bottom: 18.w,
          left: 2.w,
          right: 2.w,
          top: 2.w,
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(16.w),
          ),
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Color(0xFF6498F1).withOpacity(.14),
              offset: Offset(0, 0), //阴影y轴偏移量
              blurRadius: 1,
              spreadRadius: .3,
            )
          ],
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                item['leader_usr_logo'] != '' && item['leader_usr_logo'] != null
                    ? ClipOval(
                        child: Image.network(
                          item['leader_usr_logo'],
                          width: 88.w,
                          fit: BoxFit.cover,
                          errorBuilder: (BuildContext context, Object exception,
                              StackTrace stackTrace) {
                            return Container(
                              width: 88.w,
                              height: 88.w,
                            );
                          },
                        ),
                      )
                    : Container(
                        width: 88.w,
                        height: 88.w,
                      ),
                SizedBox(width: 30.w),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "${item['usr_name']}",
                      style: TextStyle(
                        color: Color(0xff262626),
                        fontSize: 30.sp,
                      ),
                    ),
                    SizedBox(height: 8.w),
                    Text(
                      "手机号：${item['mobile_phone']}",
                      style: TextStyle(
                        color: Color(0xff666666),
                        fontSize: 24.sp,
                      ),
                    ),
                  ],
                )
              ],
            ),
            // state.from=='invite_koc' ? Container() : Row(
            //   children: [
            //     RichText(
            //       text: TextSpan(
            //         children: [
            //           TextSpan(
            //             text: item['apply_status_num_id'] == 1
            //                 ? '去审核'
            //                 : (item['apply_status_num_id'] == 3 ? "" : '已拒绝'),
            //             style: TextStyle(
            //               color: Config.mainColor,
            //               fontSize: 28.w,
            //             ),
            //           ),
            //           WidgetSpan(
            //             child: Icon(
            //               Icons.arrow_forward_ios_outlined,
            //               color: Color(0xffADADAD),
            //               size: 16,
            //             ),
            //           ),
            //         ],
            //       ),
            //     ),
            //   ],
            // )
          ],
        ),
      ),
    );
  }
}
